import { getLog } from '@U/index';

// 测试内容是否超出
export const testAreaExceeds = () => {
  let str = '';
  let i = 0;
  while (i < 100) {
    str += '<h1>我是测试内容是否超出</h1>';
    i++;
  }
  return str;
};

// 获取小写字母数组
export const getSmallLetterList = () => {
  const smallLetterList = [];
  for (let i = 97; i < 123; i++) {
    smallLetterList.push(String.fromCharCode(i));
  }
  return smallLetterList;
};

/** generateUppercaseLetter (自动生成大写字母序号)方法说明
* @author: JasonStandFor
* @param {number} mark - 输入的下标标记，最终返回对应的标记序号
* @return mark
* @example:
generateUppercaseLetter(0);  ===> 'A'
generateUppercaseLetter(26);  ===> 'AA'
generateUppercaseLetter(52);  ===> 'AAA'
* */
export const generateUppercaseLetter = (mark) => {
  if (mark < 0) return getLog('下标不可为负数');
  const letterList = [];
  let frequency = 0;
  while (letterList.length <= mark) {
    // console.log('循环次数： ', frequency);
    for (let i = 65; i < 91; ++i) {
      letterList.push(characterSet(i, frequency));
    }
    frequency++;
  }

  function characterSet(idx, count, i = 0) {
    let letterStr = '';
    // if (count === Infinity) {
    //   console.log('计算有误，超出界限');
    //   return String.fromCharCode(idx);
    // }
    while (i <= count) {
      letterStr += `${String.fromCharCode(idx)}`;
      i++;
    }
    return letterStr;
  }
  // console.log('letterList: ', letterList);
  return letterList[mark];
};

/** randomString (生成随机字符串)方法说明
 * @author: JasonStandFor
 * @param {number} len - 生成随机数的长度
 * @description Desc:: 生成随机字符串
 * @return string
 **/
export const randomString = (len) => {
  if (len < 4) return getLog('随机长度至少为4位');
  const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
  let randomStr = '';
  for (let i = 0; i < len; i++) {
    randomStr += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return randomStr;
};

/** randomNum (生成随机数)方法说明
 * @author: JasonStandFor
 * @param {number} min - 最小值
 * @param {number} max - 最大值
 * @description Desc:: 生成随机数
 * @return number
 **/
export const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;

/** shuffleArray (打乱数组)方法说明
 * @author: JasonStandFor
 * @param {Array} array - 打乱前的数组
 * @description Desc:: 将完整的数组打乱，重排，一般做特效和抽奖用得到
 * @return Array
 **/
export const shuffleArray = (array) => {
  if (Array.isArray(array)) {
    return array.sort(() => 0.5 - Math.random());
  }
  return [];
};

/** getRandomValue (获取数组中的随机数)方法说明
 * @author: JasonStandFor
 * @param {Array} array - 奖品数组
 * @description Desc:: 获取打乱数组后的随机值，一般做特效和抽奖用得到
 * @return Array
 **/
export const getRandomValue = (array) => {
  if (Array.isArray(array)) {
    return array[Math.floor(Math.random() * array.length)];
  }
  return [];
}

/** uniqueArrayObject (数组对象根据字段去重)方法说明
 * @author: JasonStandFor
 * @param {Array} arr - 要去重的数组
 * @param {Array|string} key - 根据去重的字段名
 * @description Desc:: 数组对象根据字段去重
 * @return Array
 * @example
 例1:
 const responseList = [
 { id: 1, name: '树哥' },
 { id: 2, name: '黄老爷' },
 { id: 3, name: '张麻子' },
 { id: 1, name: '黄老爷' },
 { id: 2, name: '张麻子' },
 { id: 3, name: '树哥' },
 { id: 1, name: '树哥' },
 { id: 2, name: '黄老爷' },
 { id: 3, name: '张麻子' },
 ]

 uniqueArrayObject(responseList, 'id')
 // [{ id: 1, name: '树哥' },{ id: 2, name: '黄老爷' },{ id: 3, name: '张麻子' }]

 例2:
 const responseList = [
 { id: 1, name: '树哥', age: 1 },
 { id: 2, name: '黄老爷', age: 2 },
 { id: 3, name: '张麻子', age: 3 },
 { id: 1, name: '黄老爷' },
 { id: 2, name: '张麻子' },
 { id: 3, name: '树哥' },
 { id: 1, name: '树哥', age: 1 },
 { id: 2, name: '黄老爷', age: 2 },
 { id: 3, name: '张麻子', age: 3 },
 ]
 uniqueArrayObject(responseList, ['id', 'age'])
 // [{ id: 1, name: '树哥' },{ id: 2, name: '黄老爷' },{ id: 3, name: '张麻子' }]
 **/
export const uniqueArrayObject = (arr = [], key = 'id') => {
  if (Array.isArray(arr) && arr.length === 0) return [];
  let list = [];
  const map = {};
  if (typeof key === 'string') {
    arr.forEach((item) => {
      if (!map[item[key]]) {
        map[item[key]] = item
      }
    });
  }
  if (key && key.constructor === Array) {
    for (let i = 0, l = key.length; i < l; ++i) {
      for (let j = 0, len = arr.length; j < len; ++j) {
        if (arr[j][key[i]] && !map[arr[j][key[i]]]) {
          map[arr[j][key[i]]] = arr[j];
        }
      }
    }
  }
  list = Object.values(map);

  return list;
};
